package com.mokio.controller;

import com.github.pagehelper.PageInfo;
import com.mokio.entity.ProductInfo;
import com.mokio.entity.ProductType;
import com.mokio.service.ProductInfoService;
import com.mokio.service.ProductTypeService;
import com.mokio.vo.ProductInfoVo;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadBase;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.UUID;

/**
 * @program: xiaomishop
 * @ClassName ProductInfoController
 * @description:
 * @author: mokio
 * @create: 2024−12-10 19:12
 * @Version 1.0
 **/
@Controller
// /prod
@RequestMapping("/prod")
public class ProductInfoController {

    private static final int PAGE_SIZE = 5;

    @Autowired
    private ProductInfoService productInfoService;
    @Autowired
    private ProductTypeService productTypeService;


    /**
     * 返回分页后的商品数据
     * @param pageNo
     * @param model
     * @param session
     * @return
     */
    // /split.action 第一次进入页面时的查询
    @RequestMapping("/split.action")
    public String queryAllProductType(@RequestParam("page")Integer pageNo, Model model, HttpSession session) {
        //获取全部的 商品类型
        List<ProductType> productTypeList= productTypeService.getAllProductType();
        //将数据存入session
        session.setAttribute("ptlist",productTypeList);

        //查看页数是否合法
        Integer page = 1;
        if(pageNo != null){
            page = pageNo;
        }else {
            pageNo = page;
        }
        //调用分页
        PageInfo productInfoByPage = productInfoService.getProductInfoByPage(pageNo, PAGE_SIZE);
        //存储分页数据
        model.addAttribute("pb", productInfoByPage);
        return "product";
    }

    /**
     * 返回分页后的商品数据
     * @param vo
     * @param model
     * @param session
     * @return
     */
    // /mySplict.action 进行多条件组合查询后的数据
    @RequestMapping("/mySplict.action")
    public String queryAllProductInfo(ProductInfoVo vo, Model model, HttpSession session){

        //获取全部的 商品类型
        List<ProductType> productTypeList= productTypeService.getAllProductType();
        //将数据存入session
        session.setAttribute("ptlist",productTypeList);

        //查看页数是否合法
        Integer page = 1;
        if(vo.getPage() != null){
            page = vo.getPage();
        }else {
            vo.setPage(page);
        }
        //调用分页
        PageInfo productInfoByPage = productInfoService.getProductInfoByVo(vo, PAGE_SIZE);
        //存储分页数据
        model.addAttribute("pb", productInfoByPage);
        //加入回显功能
        session.setAttribute("vo",vo);
        return "product";
    }

    // /prod/show5.action
    @RequestMapping("show5.action")
    public String show5(Model model, HttpSession session){
        PageInfo<ProductInfo> productInfoByPage = productInfoService.getProductInfoByPage(3, PAGE_SIZE);
        //model.addAttribute("pb", productInfoByPage);
        session.setAttribute("pb", productInfoByPage);
        System.out.println("===================================================================");
        return "redirect:/main.jsp";
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @RequestMapping("/batchDel.action")
    public String batchDel(Integer[] ids){
        System.out.println();
        System.out.println();
        if(ids==null){
            return "forward:split.action?page=1";
        }

        productInfoService.batchDel(ids);
        return "forward:split.action?page=1";
    }


    //单个删除  http://localhost:8080/xiaomishops_Web_exploded/prod/delete.action?pid=1&ispage=1
    @RequestMapping("/delete.action")
    public String delete(Integer pid){
        productInfoService.deleteById(pid);
        return "forward:split.action";
    }

    //后端操作员编辑商品
    // http://localhost:8080/xiaomishops_Web_exploded/prod/one.action?pid=1&ispage=1
    @RequestMapping("/one.action")
    public String one(Integer pid,Integer ispage,HttpSession session){
        ProductInfo productInfoById = findProductInfoById(pid);
        session.setAttribute("prod",productInfoById);
        return "forward:/admin/update.jsp";
    }

    private ProductInfo findProductInfoById(Integer pid){
        ProductInfo productById = productInfoService.findProductById(pid);
        return productById;
    }
    //后端操作员新增商品
    // http://localhost:8080/xiaomishops_Web_exploded/prod/save.action
    @RequestMapping("/save.action")
    public String addProduct(@RequestParam("pimage") CommonsMultipartFile pimage, HttpServletRequest request, ProductInfo product){
        String fileName = "";

        if(!pimage.isEmpty()){
            String uploadDir = "D:\\ProjectLinkedGit\\xiaomishop\\web\\image_big"; // 指定上传路径
            File uploadDirfile = new File(uploadDir); // 创建目录对象
            fileName = pimage.getOriginalFilename(); // 获取文件名
            File destFile = new File(uploadDir, fileName); // 创建目标文件
            try {
                pimage.transferTo(destFile); // 保存文件
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        product.setpImage(fileName);//加入图片名
        productInfoService.save(product);
        return "redirect:/prod/split.action?page=1";
    }



    @RequestMapping("/update.action")
    public String updateProduct(@RequestParam("pimage") CommonsMultipartFile pimage, // 上传的图片文件
                                ProductInfo product) { // 商品信息对象

        String fileName = "";

        if(!pimage.isEmpty()){
            String uploadDir = "D:\\ProjectLinkedGit\\xiaomishop\\web\\image_big"; // 指定上传路径
            File uploadDirfile = new File(uploadDir); // 创建目录对象
            fileName = pimage.getOriginalFilename(); // 获取文件名
            File destFile = new File(uploadDir, fileName); // 创建目标文件
            try {
                pimage.transferTo(destFile); // 保存文件
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            // 如果没有上传新图片，保留原有图片
            fileName = product.getpImage();
        }

        // 更新商品信息
        product.setpImage(fileName); // 设置图片名
        productInfoService.updateByInfo(product); // 调用 Service 层更新商品信息

        // 重定向到商品列表页面，并传递当前页码
        return "redirect:/prod/split.action?page=1";
    }
    public static void main(String[] args) {
        String currentDir = System.getProperty("user.dir");
        System.out.println(currentDir);
        //设置图片保存的目录
        String path=currentDir+"\\xiaomishops\\web\\image\\";
        System.out.println(path);
    }


}
